import {
	getAuth
} from '@/api/user';
class shopUnit {
	constructor() {
	}
	navigateGoods(id,roteid) { //是否匿名进入商品详情
		if (!getAuth().username || getAuth().username == '') {
			if(roteid){
				uni.navigateTo({
					url: `/pages/goods/detailsAuth?roteid=${id}`
				})
			}else{
				uni.navigateTo({
					url: `/pages/goods/detailsAuth?id=${id}`
				})
			}
		} else {
			if(roteid){
				uni.navigateTo({
					url: `/pages/goods/details?roteid=${id}`
				})
			}else{
				uni.navigateTo({
					url: `/pages/goods/details?id=${id}`
				})
			}
		}
	}
    navigateCart(){
        if (!getAuth().username || getAuth().username == '') {
        	uni.navigateTo({
        		url: `/pages/shop/shoppingAuth`
        	})
        } else {
        	uni.navigateTo({
        		url: `/pages/shop/shopping`
        	})
        }
    }
    navigateOrder(ids){
        if (!getAuth().username || getAuth().username == '') {
        	uni.navigateTo({
        		url: `/pages/shop/orderAuth?ids=${ids}`
        	})
        } else {
        	uni.navigateTo({
        		url: `/pages/shop/order?ids=${ids}`
        	})
        }
    }
	setGoodsCart(newData) { //转换购物车格式，静态giao数据
		let historyData = this.getGoodsCart();
		let newGoodsList = '';
        let isPush=false,isPushNewGoods=false;
		let myJsonArray = { //缓存数据初始化
			status: 1,
			data: {
				goodsList: [],
				allGoodsNum: '',
				allGoodsMoney: ''
			}
		};
		if (historyData) { //如果有缓存数据，则直接进入缓存数据赋值
            myJsonArray= JSON.parse(historyData);
            // 去重操作，遍历已有缓存数据
            newGoodsList = myJsonArray.data.goodsList.map((item)=>{
                if(item.name==newData.name){//如果店铺名一样，那么直接进入操作他下面的list
                    //开始遍历该店铺下面的商品，处理后返回该商品
                    item.list = item.list.map((itemGoods)=>{
                        //检索是否有相同的产品，进行覆盖
                        
                        if(itemGoods.sku_name==newData.list[0].sku_name){
                            itemGoods = newData.list[0];
                        }else{//如果没相同的产品，那么进行新增操作
                           isPush =  item.list.every((items)=>{
                               return items.sku_name!=newData.list[0].sku_name;
                           })
                        }
                        return itemGoods;
                    })
                    if(isPush){
                         item.list.push(newData.list[0])
                    }
                }else{  //如果店铺名不一样,则直接执行新增;
                    isPushNewGoods =  myJsonArray.data.goodsList.every((itemss)=>{
                        return itemss.name!=newData.name;
                    })
                }
                 return item;
            })
            if(isPushNewGoods){
                myJsonArray.data.goodsList.push(newData)
            }else{
                myJsonArray.data.goodsList=newGoodsList;
            }
        }else{
            myJsonArray.data.goodsList.push(newData)
        }
        let price=[],goodsNum=[];
        myJsonArray.data.goodsList.forEach((item)=>{
            item.list.forEach((items)=>{
                price.push(items.moneyAll)
                goodsNum.push(items.buyNum)
            })
        })
        let newPrice = price.reduce((pre,cur)=>{
            return pre+cur
        })
        let newGoodsNum= goodsNum.reduce((pre,cur)=>{
            return pre+cur
        })
        myJsonArray.data.allGoodsNum = newGoodsNum;
        myJsonArray.data.allGoodsMoney = newPrice;
        uni.setStorageSync('goodsCart',JSON.stringify(myJsonArray))
	}
	setOrderCart(newData) { //转换购物车格式，静态giao数据
		let historyData = this.getStorage('orderCar');
		let newGoodsList = '';
	    let isPush=false,isPushNewGoods=false;
		let myJsonArray = { //缓存数据初始化
			status: 1,
			data: {
				goodsList: [],
				allGoodsNum: '',
				allGoodsMoney: ''
			}
		};
		if (historyData) { //如果有缓存数据，则直接进入缓存数据赋值
	        myJsonArray= JSON.parse(historyData);
	        // 去重操作，遍历已有缓存数据
	        newGoodsList = myJsonArray.data.goodsList.map((item)=>{
	            if(item.name==newData.name){//如果店铺名一样，那么直接进入操作他下面的list
	                //开始遍历该店铺下面的商品，处理后返回该商品
	                item.list = item.list.map((itemGoods)=>{
	                    //检索是否有相同的产品，进行覆盖
	                    
	                    if(itemGoods.sku_name==newData.list[0].sku_name){
	                        itemGoods = newData.list[0];
	                    }else{//如果没相同的产品，那么进行新增操作
	                       isPush =  item.list.every((items)=>{
	                           return items.sku_name!=newData.list[0].sku_name;
	                       })
	                    }
	                    return itemGoods;
	                })
	                if(isPush){
	                     item.list.push(newData.list[0])
	                }
	            }else{  //如果店铺名不一样,则直接执行新增;
	                isPushNewGoods =  myJsonArray.data.goodsList.every((itemss)=>{
	                    return itemss.name!=newData.name;
	                })
	            }
	             return item;
	        })
	        if(isPushNewGoods){
	            myJsonArray.data.goodsList.push(newData)
	        }else{
	            myJsonArray.data.goodsList=newGoodsList;
	        }
	    }else{
	        myJsonArray.data.goodsList.push(newData)
	    }
	    let price=[],goodsNum=[];
	    myJsonArray.data.goodsList.forEach((item)=>{
	        item.list.forEach((items)=>{
	            price.push(items.moneyAll)
	            goodsNum.push(items.buyNum)
	        })
	    })
	    let newPrice = price.reduce((pre,cur)=>{
	        return pre+cur
	    })
	    let newGoodsNum= goodsNum.reduce((pre,cur)=>{
	        return pre+cur
	    })
	    myJsonArray.data.allGoodsNum = newGoodsNum;
	    myJsonArray.data.allGoodsMoney = newPrice;
	    uni.setStorageSync('orderCar',JSON.stringify(myJsonArray))
	}
	getGoodsCart() {
		return uni.getStorageSync('goodsCart');
	}
	changeGoodsCart(goods){ //筛选购物车数据
		let goodsList = JSON.parse(goods);
		let goodsCart = JSON.parse(this.getGoodsCart());
		goodsCart.data.goodsList=goodsList;
		let price=[],goodsNum=[];
		goodsCart.data.goodsList.forEach((item)=>{
		    item.list.forEach((items)=>{
		        price.push(items.moneyAll)
		        goodsNum.push(items.buyNum)
		    })
		})
		let newPrice = price.reduce((pre,cur)=>{
		    return pre+cur
		})
		let newGoodsNum= goodsNum.reduce((pre,cur)=>{
		    return pre+cur
		})
		goodsCart.data.allGoodsNum = newGoodsNum;
		goodsCart.data.allGoodsMoney = newPrice;
		return goodsCart;
	}
	setStorage(key,value){
		uni.setStorageSync(key,value)
	}
	getStorage(key) {
		return uni.getStorageSync(key);
	}
}

class shop extends shopUnit {
	constructor(options) {
		super(options)
	}

}

export default shop;
